<?php
if (!defined('ACCESS')) {
    exit('Access denied.');
}

class Shop extends Base
{
    // 表名
    private static $table_name = 'store';
    // 查询字段
    private static $columns = array('id', 'title', 'keywords', 'description', 'width', 'height', 'min_height', 'store_footer', 'advert_imgs');

    public static function getTableName()
    {
        return self::$table_name;
    }

    public static function getInfo()
    {
        $db = self::__instance();

        $sql = "SELECT * FROM " . self::getTableName();

        $list = $db->query($sql)->fetch();

        if ($list) {
            return $list;
        }
        return array();
    }

    public static function add($data)
    {
        if (!$data || !is_array($data)) {
            return false;
        }
        $db = self::__instance();

        $id = $db->insert(self::getTableName(), $data);
        return $id;
    }

    public static function replace($value, $id)
    {
        $db = self::__instance();
        if (!is_array($value)) {
            return false;
        }

        if (isset($value['title'])) {
            $s1 = "'" . $value['title'] . "'";
        } else {
            $s1 = "''";
        }

        if (isset($value['kewords'])) {
            $s2 = "'" . $value['kewords'] . "'";
        } else {
            $s2 = "''";
        }

        if (isset($value['description'])) {
            $s3 = "'" . $value['description'] . "'";
        } else {
            $s3 = "''";
        }

        if (isset($value['width'])) {
            $s4 = "'" . $value['width'] . "'";
        } else {
            $s4 = "''";
        }

        if (isset($value['height'])) {
            $s5 = "'" . $value['height'] . "'";
        } else {
            $s5 = "''";
        }

        if (isset($value['min_height'])) {
            $s6 = "'" . $value['min_height'] . "'";
        } else {
            $s6 = "''";
        }

        if (isset($value['store_footer'])) {
            $s7 = "'" . $value['store_footer'] . "'";
        } else {
            $s7 = "''";
        }

//            $s2 = "'" . $value[1] . "'";
//            $s3 = "'" . $value[2] . "'";
//            $s4 = "'" . $value[3] . "'";
//            $s5 = "'" . $value[4] . "'";
//            $s6 = "'" . $value[5] . "'";
//            $s7 = "'" . $value[6] . "'";
//            $s8 = "'" . $value[7] . "'";

        $value_str_s = '(' . $id . ',' . $s1 . ',' . $s2 . ',' . $s3 . ',' . $s4 . ',' . $s5 . ',' . $s6 . ',' . $s7 . ')';


        $sql = "REPLACE INTO store (`id`,`title`,`kewords`,`description`,`width`,`height`,`min_height`,`store_footer`)
VALUES " . $value_str_s;

        $rt = $db->exec($sql);
        return $rt;
    }

    public static function updateInfo($data, $id)
    {

        if (!$data || !is_array($data)) {
            return false;
        }
        $db = self::__instance();
        $condition = array("id" => $id);

        $id = $db->update(self::getTableName(), $data, $condition);
        return $id;
    }

    public static function getAdvertImgs()
    {

        $db = self::__instance();

        $sql = "SELECT * FROM " . self::getTableName();

        $list = $db->query($sql)->fetch();

        if ($list) {
            return $list;
        }
        return array();
    }


    public static function updateSpec($data)
    {

        $db = self::__instance();

        $sql = "UPDATE  " . self::getTableName() . " SET `advert_imgs` = '$data'";

        $ret = $db->exec($sql);


        return $ret;
    }
}